package com.haiyi.activiti.engine.framework.activiti.manager.impl;

import com.haiyi.activiti.engine.framework.activiti.manager.DeployManager;
import com.haiyi.activiti.engine.util.Assert;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;

/**
 * <p> </p>
 *
 * <pre> Created: 2018/12/5 16:02 </pre>
 *
 * @author hlu
 * @version 1.0
 * @since JDK 1.7
 */
@Component
@Transactional(rollbackFor = Exception.class)
public class DeployManangerImpl implements DeployManager {
    @Autowired
    private RepositoryService repositoryService;

    @Override
    public Deployment deployByClassPath(String classpath) {
        final Deployment deploy = repositoryService
                .createDeployment()
                .addClasspathResource(classpath)
                .deploy();
        Assert.isNotNull(deploy,"发布失败！");
        return deploy;
    }

    @Override
    public Deployment deployByInputStream(String resourceName,InputStream inputStream) {
        final Deployment deploy = repositoryService
                .createDeployment()
                .addInputStream(resourceName,inputStream)
                .deploy();
        Assert.isNotNull(deploy,"发布失败！");
        return deploy;
    }

    @Override
    public Deployment deployByString(String resourceName,String text) {
        final Deployment deploy = repositoryService
                .createDeployment()
                .addString(resourceName,text)
                .deploy();
        Assert.isNotNull(deploy,"发布失败！");
        return deploy;
    }

    @Override
    public void deleteDeployment(String deployId, boolean cascade) {
        repositoryService.deleteDeployment(deployId,cascade);
    }
}
